class PeopleController < ApplicationController
  layout :people_layout
  before_filter :check_authentication

  def people_layout
    if params[:project_id]
      "project"
    else
      "company"
    end
  end
  def index
    @company = @cur_user.company
    if @project
      @us = @company.project_users(params[:project_id]) - [@company.account_owner]
      @us_no = @company.users - @company.project_users(params[:project_id]) - [@company.account_owner]
    else
      @us = @company.users - [@company.account_owner]
      @us_no = @company.removed_users
    end
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end
  def create_child_company
    c = Company.new(params[:child_company])
    c.save
    @cur_user.company.children << c
    if @project
    redirect_to "/projects/#{@project.id}/people"
    else
    redirect_to "/people"
    end
  end
  def create
    @user = User.new(params[:user])
    @company = @cur_user.company
    @user.company = @company
   if @user.save
     if @user.email.match("@")
       Notifier.deliver_notify('signup',@user.email,@user,@user.lang)
     end
    if @project
    task = Task.new
    task.user = @user
    task.project_id  = params[:project_id]
    task.role = "Employee"
    task.save
      @us = @company.project_users(params[:project_id]) - [@company.account_owner]
      @us_no = @company.users - @company.project_users(params[:project_id]) - [@company.account_owner]
    else
      if params[:project_ids]
	params[:project_ids].each do |p|
	  task = Task.new
	  task.project_id = p
	  task.user_id = @user.id
          task.role = "Employee"
          task.save
        end
      end
      @us = @company.users - [@company.account_owner]
      @us_no = @company.removed_users
    end
    render :update do |page|
	  page.visual_effect :BlindUp,'new_user' 
	  page.hide 'indicator'
	  page.replace_html 'user_list', :partial=>'user_list'
	  page.visual_effect :highlight,'td_' + @user.id.to_s,:duration=>25
	end 
   end 
  end

  def add_person
    task = Task.new
    task.user = User.find(params[:person])
    task.project_id  = params[:project_id]
    task.role = "Employee"
    task.save

    @us = @cur_user.company.project_users(params[:project_id]) - [@cur_user.company.account_owner]
    @us_no = @cur_user.company.users - @us - [@cur_user.company.account_owner]
    render :update do |page|
	  page.visual_effect :BlindUp,'new_user' 
	  page.hide 'indicator2'
	  page.replace_html 'user_list', :partial=>'user_list',:locals=>{:project_id=>params[:project_id]}
	  page.replace_html 'us_no', :partial=>'us_no',:locals=>{:us_no=>@us_no,:project_id=>params[:project_id]}
	  page.visual_effect :highlight,'td_' + params[:person].to_s,:duration=>15
    end 
  end

  def remove
    pid = params[:project_id]
    uid = params[:uid]
    Task.find(:first,:conditions=>"user_id = #{uid} and project_id = #{pid}").destroy
    user = User.find(uid)
    #TODO : remove project roles for user 
    #TODO : remove project roles for user 
    #TODO : remove project roles for user 
    #TODO : remove project roles for user 
    @us = @cur_user.company.project_users(pid) - [@cur_user.company.account_owner]
    @us_no =@cur_user.company.users - @us - [@cur_user.company.account_owner]
    render :update do |page|
	  #page.hide 'indicator'
	  page.replace_html 'us_no', :partial=>'us_no',:locals=>{:us_no=>@us_no,:project_id=>pid}
	  page.replace_html 'user_list', :partial=>'user_list',:locals=>{:project_id=>pid}
          page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'}
    end
  end
  def set_administrator
    user = User.find(params[:id])
    if user.role == "Administrator"
    user.role = "Emaployee"
    else
    user.role = "Administrator"
    end
    user.save
    render :update do |page|
    page.replace_html 'td_' + user.id.to_s, :partial=>'people',:locals=>{:people=>user}
      page.visual_effect :highlight, 'td_' + user.id.to_s, :duration=>'5'
    end
  end

  def change_role
    user = User.find(params[:id])
    role = Role.find(params[:role_id])
    if user.roles.size == 0
        user.roles << role
    elsif user.roles.size == 1
      if user.roles.first.identifier.match("custom")
        user.roles << role
      else
        user.roles.clear
        user.roles << role
      end
    elsif user.roles.size == 2
        user.roles.clear
        user.roles << role
        user.roles << Role.find_by_identifier("custom.#{user.id}.role")
    end

    render :update do |page|
    page.replace_html 'td_' + user.id.to_s, :partial=>'people',:locals=>{:people=>user}
    page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'}
      page.visual_effect :highlight, 'td_' + user.id.to_s, :duration=>'5'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    user = User.find(params[:id])
    if user.update_attributes(params[:user])
      #set roles 
      if params[:user][:role].to_i > 0 
        role = Role.find(params[:user][:role])
        if user.roles.size == 0
           user.roles << role
         elsif user.roles.size == 1
           if user.roles.first.identifier.match("custom")
              user.roles << role
           else
              user.roles.clear
              user.roles << role
           end
        elsif user.roles.size == 2
           user.roles.clear
           user.roles << role
           user.roles << Role.find_by_identifier("custom.#{user.id}.role")
        end
      end

      if params[:project_id].to_i > 0 
        redirect_to "/projects/#{@project.id}/people"
      else
        redirect_to people_path
      end
    else
      redirect_to edit_person_path(@user)
    end
  end

  def update_company
    @company = Company.find(params[:id])
    if @company.update_attributes(params[:company])
      flash[:notice] = 'Company was successfully updated.'
      redirect_to :action => 'list'
    else
      render :action => 'edit'
    end
  end
  def send_welcome_email
    @user = User.find(params[:id])
    Notifier.deliver_notify('signup',@user.email,@user,@user.lang)
  end

  def destroyb
    u = User.find(params[:id])
	u.time_logs.each do |t|
	  t.destroy
	end
	u.tasks.each do |t|
	  t.destroy
	end
	u.destroy
    @s_user = session[:user]
	@s_user.reload
	@company = @s_user.company
    @user = User.new
    render :update do |page|
	   page.replace_html 'userlist',:partial=>'user'
    end
  end
  def destroy
    pid = params[:project_id]
    uid = params[:uid]
    u = User.find(params[:uid])
    u.tasks.each do |t|
      t.destroy
    end
    u.roles.clear
    u.destroyed = true
    u.save
    if @project
    @us = @cur_user.company.project_users(@project.id) - [@cur_user.company.account_owner]
    @us_no =@cur_user.company.users - @us - [@cur_user.company.account_owner]
    else
    @us = @cur_user.company.users -  [@cur_user.company.account_owner]
    @us_no = @cur_user.company.removed_users
    end
    render :update do |page|
	  #page.hide 'indicator'
          if @project
	  page.replace_html 'us_no', :partial=>'us_no',:locals=>{:us_no=>@us_no}
          end
	  page.replace_html 'user_list', :partial=>'user_list'
          page.replace_html 'cat_side_show',:partial=>'/permission/role_list',:locals=>{:project=>params[:project_id],:label=>'ss'}
    end
  end
end
